package com.sz.util;

import org.springframework.context.annotation.Configuration;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.Cipher;

/**
 * @author liujq
 */
@Configuration
public class RsaCryptUtil {

    /**
     * RSA加密秘钥
     * @param randKey 要加密的AES秘钥
     * @param publicKey RSA公钥
     * @return String 加密后的AES秘钥
     */
    public  String rsaEncryptRandKey(String randKey, String publicKey) throws Exception {
        byte[] decoded = Base64.getDecoder().decode(publicKey.getBytes());
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(randKey.getBytes("UTF-8")));
    }

    /**
     * RSA解密秘钥
     * @param encRandKey 要解密的AES秘钥
     * @param privateKey RSA私钥
     * @return String 解密后的AES秘钥
     */
    public  String rsaDescryptRandKey(String encRandKey, String privateKey) throws Exception {
        byte[] keyBytes = Base64.getDecoder().decode(privateKey.getBytes());
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PrivateKey priKey = keyFactory.generatePrivate(keySpec);
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        byte[] bytes = Base64.getDecoder().decode(encRandKey);
        return new String(cipher.doFinal(bytes));
    }

    /**
     * 公钥
     * @return
     */
    public String getPublicRSAKey(){
        String publicRSAkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC6fo7TTyHygNKsGBuwJOOt73I8Vwg8/SCTZdGA2RqdqLufccpYpLZIWU+nVshSG4kAViqIxzyj8rPz/XvOlAvr1I11pGxN5ZuYYWgPIKCBkW50jyIOXbfBnaX2pquckhGh8UwzIBF0ynTXwT+bDvo3zzGYq2457JbQmSiyPHIzmwIDAQAB";
        return publicRSAkey;
    }

    /**
     * 私钥
     * @return
     */
    public String getPrivateRSAKey(){
        String privateRSAKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALp+jtNPIfKA0qwYG7Ak463vcjxXCDz9IJNl0YDZGp2ou59xyliktkhZT6dWyFIbiQBWKojHPKPys/P9e86UC+vUjXWkbE3lm5hhaA8goIGRbnSPIg5dt8Gdpfamq5ySEaHxTDMgEXTKdNfBP5sO+jfPMZirbjnsltCZKLI8cjObAgMBAAECgYBz6gGf+UliTwuih4F+JA+XpoJcWsP2FGbQ9rIW4doXMC8d1T4thoL9yPUnT1tHHXIz0VqylRNt24ZaGq1/IqmdJWCFiQkjLlAVyLxwv9/Zm5FsvJdFI+tcpN3NI4RBAgT7+ARPXHHwJ9gfzi4/oh0nM154BQRi3y2ExmxGvOuc8QJBAN5cgK6f4svdMjgjPwdQT4rq7yTH2dy9+aJYyWQdm1AKmTrCpmSpn4Y8koWKoj3duQQM3b2fwAA+cUQH9nI5ZW0CQQDWtQRvkPBW0qXOp6lYeHIbj5kv2sQgVuD7DBrwKppp7DLTnvwA+AMolzJG19C7HCkEWULoTHJvQ99ATnDk6sAnAkB0cdyxleke+WcjHtIS8mu1Q/KYQFBG2PWxnu1YEGkVMXes3eJlNK7ljTLt2/u6ijIpfNNiShYyCoEmL0VP697BAkB/gbDIsMKKTT66o0cmiyD4zSakDL88lVu2u1GkHyjkjqkAlAIEcUUu7dulxrTm6apSMtBRNFUhTqVegzb4O5ExAkEAq+jxHX4qYh3Hz8auQUfKBmGDvurjTa1nkRJUU9m/X00/gX5brkFlIKvFoib+DwWW1QxWCp6wsa9jHuKSWKSfxw==";
        return privateRSAKey;
    }

}
